import BaseCURDApi from '@/framework/infra/base/BaseCURDApi'
import request from '@/utils/request'
import ComplainFollowUpStatusEnum from '@/feature/cmt/infra/constants/ComplainFollowUpStatusEnum'
import ApiAdaptor from '@/framework/infra/base/ApiAdaptor'

class ComplainFollowUpApi extends BaseCURDApi {
  constructor() {
    super('cmt/complain-follow-up')
  }

  myFollowCount() {
    return this.get('my-follow-count')
  }

  dataCount() {
    return this.get('data-count').then(async data => {
      if (!data) {
        return {}
      }
      return this.parseDataCount(data)
    })
  }

  parseDataCount(data) {
    const map = data.reduce((r, a) => {
      let status = a.status
      if (status === ComplainFollowUpStatusEnum.ING.id) {
        status = ComplainFollowUpStatusEnum.WAIT_FOLLOW.id
      }
      r[status] = r[status] || 0
      r[status] += a.count
      return r
    }, {})
    const result = ComplainFollowUpStatusEnum.getKeys().reduce((r, a) => {
      r[a] = r[a] || 0
      r[a] += map[ComplainFollowUpStatusEnum[a].id] || 0
      return r
    }, {})
    // result['myFollowCount'] = await this.myFollowCount()
    return result
  }

  listMyRegister(data) {
    return this.get('list-my-register', data)
  }

  listCanFollow(data) {
    return this.get('list-can-follow', data)
  }

  listMyFollow(data) {
    return this.get('list-my-follow', data)
  }

  listCanCheck(data) {
    return this.get('list-can-check', data)
  }

  list(params) {
    const getParams = {...params}
    Object.keys(getParams).forEach(key => {
      if (Array.isArray(getParams[key])) {
        getParams[key] = getParams[key].join(',')
      }
    })
    return this.get('list', getParams)
  }

  remove(id) {
    return this.post('remove', { id })
  }

  create(createParam) {
    return this.post('create', createParam)
  }

  update(data) {
    return this.post('update', data)
  }

  saveTbLink(id, tbLink) {
    return this.post('save-tb-link', { id, tbLink })
  }

  submit(id) {
    return this.post('submit', { id })
  }

  cancel(id) {
    return this.post('cancel', { id })
  }

  follow(data) {
    return this.post('follow', data)
  }

  async findLatestByItemNo(itemNo, maxId) {
    if (itemNo.length < 5) {
      return null
    }
    return this.get('find-latest-by-item-no', { itemNo, maxId })
  }

  export({ exportOption, filter }) {
    return request.post(this.absUrl('export'), {
      exportOption,
      filter
    })
  }

  check({ checkStatus, checkRemark = '', idList, ...data }) {
    return this.post('check', {
      ...data,
      status: checkStatus === 5 ? 1 : 0, remark: checkRemark, id: idList[0]
    })
  }

  followReject(data) {
    return this.post('follow-reject', data)
  }

  queryRevocationDataByStore(data = []) {
    const param = {}
    if (data && data.length) {
      param.startDate = data[0]
      param.endDate = data[1]
    }
    return this.get('revocation-data-by-store', param)
  }

  queryComplainFollowUpDataByStore(data = []) {
    const param = {}
    if (data && data.length) {
      param.startDate = data[0]
      param.endDate = data[1]
    }
    return this.get('complain-data-by-store', param).then(data => {
      if (data && data.length) {
        const item = {
          storeName: '汇总',
          reduceScore: data.reduce((r, a) => r + a.reduceScore, 0),
          complainCount: data.reduce((r, a) => r + a.complainCount, 0),
          waitHandleCount: data.reduce((r, a) => r + a.waitHandleCount, 0),
          backCount: data.reduce((r, a) => r + a.backCount, 0),
          doneCount: data.reduce((r, a) => r + a.doneCount, 0),
          payOutAmount: data.reduce((r, a) => r + a.payOutAmount, 0),
          tmPayOutAmount: data.reduce((r, a) => r + a.tmPayOutAmount, 0)
        }
        item.backRate = item.complainCount > 0 ? (item.backCount / item.complainCount * 100).toFixed(2) : 0
        data.push(item)
      }
      return data
    })
  }

  // check(data) {
  //   return this.post('check', data)
  // }
}

export const complainFollowUpApi = new ComplainFollowUpApi()
